#include<bits/stdc++.h>
using namespace std;

signed main() {
	int t, beginHour, beginMin, cost, tmp;
	scanf("%d", &t);
	while (t--) {
		scanf("%d:%d%d%d", &beginHour, &beginMin, &cost, &tmp);
		int arriveTime = beginHour * 60 + beginMin;
		arriveTime += cost;
		int index = 1;
		int beginTime = 10 * 60;
		while (beginTime <= 1440) {
			if (arriveTime <= beginTime) {
				cout << index << " " << beginTime - arriveTime << endl;
				break;
			}
			beginTime += tmp;
			index++;
		}
	}
	return 0;
}
